Imports System.Data.SqlClient

Public Class cls_cotizacion_

    Implements ICloneable

#Region "Variables ..."
    Private int_idcotizacion As Integer = 0
    Private int_idmoneda As Integer = 0
    Private dte_fecha As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private dbl_valor As Double = 0

    Private col_Items As Collections.Generic.List(Of cls_cotizacion_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idcotizacion
        End Get
    End Property
    Property idcotizacion() As Integer
        Get
            idcotizacion = int_idcotizacion
        End Get
        Set(ByVal value As Integer)
            int_idcotizacion = value
        End Set
    End Property
    Property idmoneda() As Integer
        Get
            idmoneda = int_idmoneda
        End Get
        Set(ByVal value As Integer)
            int_idmoneda = value
        End Set
    End Property
    Property fecha() As DateTime
        Get
            fecha = dte_fecha
        End Get
        Set(ByVal value As DateTime)
            dte_fecha = value
        End Set
    End Property
    Property valor() As Double
        Get
            valor = dbl_valor
        End Get
        Set(ByVal value As Double)
            dbl_valor = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_cotizacion_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_cotizacion_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_cotizacion_)
        While obj_Reader.Read()
            Dim Elemento As New cls_cotizacion_
            Elemento.int_idcotizacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcotizacion")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda"))) = False Then
                Elemento.int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
                Elemento.dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
                Elemento.dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idcotizacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcotizacion")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda"))) = False Then
            int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
            dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
            dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__Nuevo"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@valor", SqlDbType.Float).Value = dbl_valor
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__Modificar"
                .Parameters.Add("@idcotizacion", SqlDbType.Int).Value = int_idcotizacion
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idcotizacion As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__Eliminar"
                .Parameters.Add("@idcotizacion", SqlDbType.Int).Value = idcotizacion
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idcotizacion As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__Buscar"
                .Parameters.Add("@idcotizacion", SqlDbType.Int).Value = idcotizacion
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_cotizacion_Clon As New cls_cotizacion_
        obj_cotizacion_Clon.int_idcotizacion = Me.int_idcotizacion
        obj_cotizacion_Clon.int_idmoneda = Me.int_idmoneda
        obj_cotizacion_Clon.dte_fecha = Me.dte_fecha
        obj_cotizacion_Clon.dbl_valor = Me.dbl_valor
        obj_cotizacion_Clon.col_Items = Me.col_Items
        Return obj_cotizacion_Clon
    End Function

    Function CargarXidmoneda(ByVal int_idmoneda As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidmoneda = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__CargarXidmoneda"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidmoneda = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidmoneda los registros de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Function BuscarUltimaXidmoneda(ByVal int_idmoneda As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarUltimaXidmoneda = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cotizacion__BuscarUltimaXidmoneda"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarUltimaXidmoneda = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al BuscarUltimaXidmoneda el registro de cotizacion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

End Class
